#http://ydyl.drcnet.com.cn
from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
import json
import requests
import time
from bs4 import BeautifulSoup
import time
import re
import json
import urllib
proxies = {
            "http": "http://qf:qf3.1415@202.112.118.7:10086/"
        }
#返回结果格式
def resultData():
    data = {
        "websiteType" : 0,
        "titleName":"",
        "titleHref": "",
        "source":"",
        "label":"",
        "volume":"",
        "summary":"",
        "detailId":'',
        "publishTime" :"",
        "category" : "",
        "quote" :"",
        "abstract": "",
        "doi": "",
        "downLoad": "",
        "keyword": [],
        "author": [],

    }
    return data

def formData(response):
    allData = []
    # 获取sid并且更新返回

    lis = response["data"]


    for li in lis:
        tempData = resultData()

        try:
            tempData["titleName"] = li["info"]["mddata_name"]
        except:
            pass

        try:
            tempData["abstract"] = li["info"]["raw_text_simp"]
        except:
            pass

        allData.append(tempData)

    return allData

def getData(keyword):

    headers = {
        'Host': 'baodingcc-admin.datahistory.cn',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Accept': '*/*',
        'Referer': 'http://baodingcc.datahistory.cn/pc/text_search_3.html?words=%u540D%u518C',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    params = (
        ('callback', 'jQuery111007851210742786596_1551079377706'),
        ('mysessions', 'r4h39vwtpc'),
        ('sqlname', ['baoding', 'baoding']),
        ('my', ['0', '0']),
        ('zid', ['0', '0']),
        ('action', 'text_search'),
        ('m', 'app_search3'),
        ('keywords', keyword),
        ('p', '1'),
        ('s', '20'),
        ('_', '1551079377707'),
    )

    response = requests.get('http://baodingcc-admin.datahistory.cn/pad_index.php', headers=headers, params=params,)

    px = re.compile("\((.*)\)")
    data = px.findall(response.text)
    response = json.loads(data[0])

    allData = formData(response)

    return allData

def getCookies():

    headers = {
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
    }
    s = requests.session()

    response = s.get('http://tt.sciinfo.cn/ThinkTank/thinkindex.aspx', headers=headers,proxies = proxies)

    cookies = s.cookies.get_dict()

    return cookies

def getOneCookies():

    headers = {
        'Proxy-Connection': 'keep-alive',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
    }

    s = requests.session()

    s.get('http://cndata.datesdata.com.cn/', headers=headers,proxies = proxies)

    cookies = s.cookies.get_dict()

    return cookies


def index(request):

    data = json.loads(request.POST.get('data', '1'))

    # cookies = getCookies()

    allData = getData(data["values"][0])

    return HttpResponse(json.dumps({
        "collection": allData,
        "allNum": 0
    }), content_type="application/json")